﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TongTongMall.MallWeb.Application.Configuration;
using ZXing;
using ZXing.QrCode;

namespace TongTongMall.MallWeb.Application
{
    public static class UnitService
    {
        /// <summary>
        /// 生成数字验证码
        /// </summary>
        /// <param name="length">长度</param>
        /// <returns>验证码</returns>
        public static string CreateNumVCode(int length)
        {
            string vcode = string.Empty;
            Random r = new Random();
            do
            {
                vcode += r.Next(0, 10).ToString();
            } while (vcode.Length < length);
            return vcode;
        }

        public static byte[] CreateQRCode(string value, QrCodeEncodingOptions options = null)
        {
            BarcodeWriter writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            if (options == null)
            {
                options = new QrCodeEncodingOptions();
                options.DisableECI = true;
                //设置内容编码
                options.CharacterSet = "UTF-8";
                //设置二维码的宽度和高度
                options.Width = 500;
                options.Height = 500;
                //设置二维码的边距,单位不是固定像素
                options.Margin = 1;
                options.ErrorCorrection = ZXing.QrCode.Internal.ErrorCorrectionLevel.M;
            }
            writer.Options = options;

            using (MemoryStream stream = new MemoryStream())
            {
                Bitmap map = writer.Write(value);

                //string filename = @"H:\桌面\截图\generate1.png";
                map.Save(stream, ImageFormat.Png);
                map.Dispose();
                return stream.ToArray();
            }
        }

        /// <summary>
        /// 图片地址拼接
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public static string ImageUrl(this string uri)
        {
            if (string.IsNullOrWhiteSpace(uri))
            {
                return uri;
            }
            if (uri.StartsWith("http://") || uri.StartsWith("https://"))
            {
                return uri;
            }
            return ApplicationDefaultInstance.Instance.ApiConfig.Image7NiuServerUrl + uri + "?imageView2/4/quality/30";
            // Instance.ApiConfig.Image7NiuServerUrl
        }

        /// <summary>
        /// 视频地址拼接
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public static string VideoUrl(this string uri)
        {
            if (string.IsNullOrWhiteSpace(uri))
            {
                return uri;
            }
            if (uri.StartsWith("http://") || uri.StartsWith("https://"))
            {
                return uri;
            }
            return ApplicationDefaultInstance.Instance.ApiConfig.Image7NiuServerUrl + uri;
            // Instance.ApiConfig.Image7NiuServerUrl
        }

        /// <summary>
        /// 图片地址拼接
        /// </summary>
        /// <param name="uri"></param>
        /// <returns></returns>
        public static string ImageUrl(this string uri, string imgServerUrl)
        {
            if (string.IsNullOrWhiteSpace(uri))
            {
                return uri;
            }
            if (uri.StartsWith("http://") || uri.StartsWith("https://"))
            {
                return uri;
            }
            return imgServerUrl + uri + "?imageView2/4/quality/30";
            // Instance.ApiConfig.Image7NiuServerUrl
        }
    }
}
